﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace PanelLogowania
{
    public partial class Quiz : Form
    {
        int[] Odpowiedzi;
        int MaxPytan;
        string[] IdPytan;
        public Quiz(int IdKategoriii, int IloscPytann)
        {
            int MaxPytann = 0;
            IdKategorii = IdKategoriii;
            IloscPytan = IloscPytann;
            string MyConString = "SERVER=178.162.175.80;" +
            "PORT=3306;" +
            "DATABASE=quiz;" +
            "UID= bg;" +
            "PASSWORD=2Yftm4EvRME4YWFM;";
            MySqlConnection connection = new MySqlConnection(MyConString);
            MySqlCommand command = connection.CreateCommand();
            MySqlDataReader Reader;
            command.CommandText = "SELECT name from categories WHERE id=" + IdKategorii.ToString();
            try
            {
                connection.Open();
                Reader = command.ExecuteReader();
                while (Reader.Read())
                {
                    string thisrow = "";
                    thisrow += Reader.GetValue(0).ToString();
                    textBoxKategoria.Text = thisrow;
                }
                command.CommandText = "SELECT COUNT(*) FROM questions WHERE cat_id=" + IdKategorii.ToString();
                Reader = command.ExecuteReader();
                while (Reader.Read())
                {
                    string thisrow = "";
                    thisrow += Reader.GetValue(0).ToString();
                    MaxPytann = (int)Convert.ToDouble(thisrow);
                }
                command.CommandText = "SELECT id FROM questions WHERE cat_id=" + IdKategorii.ToString();
                Reader = command.ExecuteReader();
                int i = 0;
                if (MaxPytann < IloscPytan)
                    IloscPytan = MaxPytann;
                IdPytan = new string[IloscPytan];
                while (Reader.Read())
                {
                    string thisrow = "";
                    thisrow = Reader.GetValue(0).ToString();
                    IdPytan[i] = thisrow;
                    i++;
                }
                Odpowiedzi = new int[IloscPytan];
                command.CommandText = "SELECT content FROM questions WHERE id=" + IdPytan[0].ToString();
                Reader = command.ExecuteReader();
                while (Reader.Read())
                {
                    string thisrow = "";
                    thisrow += Reader.GetValue(0).ToString();
                    textBoxPytanie.Text = thisrow;
                }
                command.CommandText = "SELECT ans1, ans2, ans3, ans4 FROM questions WHERE id=" + IdPytan[0].ToString();
                Reader = command.ExecuteReader();
                i = 0;
                while (Reader.Read())
                {
                    string thisrow = "";
                    thisrow = Reader.GetValue(0).ToString();
                    switch (i)
                    { 
                        case 0:
                            textBoxOdpA.Text = thisrow;
                            break;
                        case 1:
                            textBoxOdpB.Text = thisrow;
                            break;
                        case 2:
                            textBoxOdpC.Text = thisrow;
                            break;
                        case 3:
                            textBoxOdpD.Text = thisrow;
                            break;
                    }
                    i++;
                }

                connection.Close();
                for (i = 0; i <= IloscPytan; i++)
                    comboBoxWybor.Items.Add(i);

            }
            catch (MySqlException ex)
            {
                MessageBox.Show(ex.Message);
            }
            InitializeComponent();
        }

        int IdKategorii, IloscPytan;

        private void Quiz_Load(object sender, EventArgs e)
        {

        }
    }
}
